import 'package:flutter/material.dart';

import '../widget/custom_dialog.dart';

//toast包
// import 'package:fluttertoast/fluttertoast.dart';


class Dialog extends StatefulWidget {
  const Dialog({Key? key}) : super(key: key);

  @override
  _DialogState createState() => _DialogState();
}

class _DialogState extends State<Dialog> {
  // AlertDialog弹窗组件
  void alertDialogSync() async {
    var res = await showDialog(
        barrierDismissible: false, //点击遮罩层是否关闭弹窗，默认true，即关闭
        context: context,
        builder: (context) {
          return AlertDialog(
            title: const Text('提示'),
            content: const Text('弹窗主体区域'),
            actions: [
              ElevatedButton(
                  onPressed: () {
                    //关闭弹窗，pop参数是函数返回值
                    Navigator.of(context).pop('点击取消');
                  },
                  child: const Text('取消')),
              ElevatedButton(
                  onPressed: () {
                    //关闭弹窗，pop参数是函数返回值
                    Navigator.of(context).pop('点击确定');
                  },
                  child: const Text('确定'))
            ],
          );
        });
    //pop抛出的结果
    print(res);
  }

  // SimpleDialog弹窗组件
  void simpleDialogSync() async {
    var res = await showDialog(
      barrierDismissible: false, //点击遮罩层是否关闭弹窗，默认true，即关闭
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: const Text('提示'),
            children: [
              SimpleDialogOption(
                child: const Text('选项A'),
                onPressed: () {
                  Navigator.of(context).pop('选项A');
                },
              ),
              const Divider(), //分割线
              SimpleDialogOption(
                child: const Text('选项B'),
                onPressed: () {
                  Navigator.of(context).pop('选项B');
                },
              ),
              const Divider(), //分割线
              SimpleDialogOption(
                child: const Text('选项C'),
                onPressed: () {
                  Navigator.of(context).pop('选项C');
                },
              )
            ],
          );
        });

    print(res);
  }

  // 底部弹窗框
  void showModalBottomSheetSync() async {
    var res = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 200,
            child: Column(
              children: [
                ListTile(
                    title: const Text('分享QQ'),
                    onTap: () {
                      Navigator.of(context).pop('分享QQ');
                    }),
                ListTile(
                    title: const Text('分享微信'),
                    onTap: () {
                      Navigator.of(context).pop('分享微信');
                    }),
                ListTile(
                    title: const Text('分享短信'),
                    onTap: () {
                      Navigator.of(context).pop('分享短信');
                    }),
              ],
            ),
          );
        });
    print(res);
  }
  
  //自定义弹窗
  void customDialog(){
    showDialog(
      barrierDismissible: false, //点击遮罩层是否关闭弹窗，默认true，即关闭
      context: context, builder: (context){
      return const CustomDialog();
    });
  }
  

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('弹窗示例')),
        //居中布局。Center被拉伸到全屏，Column放置在中间的一列，主轴（竖直）居中。
        //补充Column：（一般写法中，都会在Column组件外部套上Container、SizeBox、Center容器，用来约束列组件的范围。Center用于子组件居中，SizeBox设置子组件区域宽高，Container不仅可以设置子组件的区宽高，还能设置padding等更多细节）
        body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
            ElevatedButton(
              child: const Text('AlertDialog弹窗'),
              onPressed: alertDialogSync,
            ),
            ElevatedButton(
              child: const Text('SimpleDialog弹窗'),
              onPressed: simpleDialogSync,
            ),
            ElevatedButton(
              child: const Text('向上弹出的弹窗'),
              onPressed: showModalBottomSheetSync,
            ),
            ElevatedButton(
              child: const Text('自定义的弹窗'),
              onPressed: customDialog,
            ),
            ElevatedButton(
              child: const Text('toast提示'),
              onPressed:  () {
                // Fluttertoast.showToast(
                //     msg: "This is Center Short Toast",
                //     toastLength: Toast.LENGTH_SHORT,
                //     gravity: ToastGravity.CENTER,
                //     timeInSecForIosWeb: 1,
                //     backgroundColor: Colors.black,
                //     textColor: Colors.white,
                //     fontSize: 16.0
                // );
              //toast 
              //插件地址：https://pub.dev/packages/fluttertoast
              //安装插件：flutter pub add fluttertoast 
              //import 'package:fluttertoast/fluttertoast.dart';
            },
            )
          ]),
        ));
  }
}
